package com.example.peachgrove.controller.admin.login;


import com.example.peachgrove.utils.JWTUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@Controller
@ResponseBody
@Api(tags = "用户授权")
public class login {

    @Value("${Login.password}")
    private String password;
    @Value("${Login.username}")
    private String username;

    @Autowired
    JWTUtils jwtUtils;

    //用户授权
    @PostMapping("/login")
    @ApiOperation("用户token授权")
    @ApiImplicitParams({
            @ApiImplicitParam(name="username",value="账号",dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name="password",value="密码",dataTypeClass = String.class, paramType = "query")
    })
    public HashMap login(HttpServletRequest request){
        HashMap res = new HashMap<>();
        if(request.getParameter("username").equals(username) && request.getParameter("password").equals(password)){
            String token = jwtUtils.getToken(request.getParameter("username"),
                    request.getParameter("password"));
            res.put("token",token);
            res.put("msg","登陆成功");
            res.put("code","1");
        }else {
            res.put("token","null");
            res.put("msg","登陆失败");
            res.put("code","0");
        }
        return res;
    }
}
